package cn.edu.just.firstjavaapp.l17;

public class Main03 {
    public static void main(String[] args) throws InterruptedException {
        Counter counter = new Counter();
        IncrThread incrThread = new IncrThread(counter,1000);
        DecrThread decrThread = new DecrThread(counter,1000);
        incrThread.start();

        decrThread.start();
//        incrThread.join();
//        System.out.println(counter.getCounter());
//        decrThread.join();
        Thread.sleep(1000);
        System.out.println(counter.getCounter());
    }
}

class IncrThread extends Thread{

    Counter counter;
    int times;

    public IncrThread(Counter counter,int times){
        this.counter = counter;
        this.times = times;
    }

    public void run(){
        for(int i=0;i<times;i++){
            counter.increment();
        }
    }
}

class DecrThread extends Thread{

    Counter counter;
    int times;

    public DecrThread(Counter counter,int times){
        this.counter = counter;
        this.times = times;
    }

    public void run(){
        for(int i=0;i<times;i++){
            counter.decrement();
        }
    }
}
